package com.exwave.socialgame.model_user.controller;

import com.exwave.socialgame.jwt_token.Token;
import com.exwave.socialgame.model_user.entity.Sign;
import com.exwave.socialgame.model_user.entity.User;
import com.exwave.socialgame.model_user.service.SignService;
import com.exwave.socialgame.jwt_token.TokenService;
import com.exwave.socialgame.myutils.RedisForUser;
import com.exwave.socialgame.myutils.ReturnUtils;
import com.exwave.socialgame.myutils.ParamsException;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * (Sign)表控制层
 *
 * @author makejava
 * @since 2021-10-24 12:55:53
 */
@RestController
@RequestMapping("sign")
public class SignController {
    /**
     * 服务对象
     */
    @Resource
    private SignService signService;
    @Resource
    private TokenService tokenService;
    @Resource
    private HttpServletRequest httpServletRequest;
    @Resource
    private RedisForUser redisForUser;

    /**
     * 通过主键查询单条数据
     *
     * @param sid 主键
     * @return 单条数据
     */

    @GetMapping("query")
    @Token.UserLoginToken
    @ApiOperation(value = "查询登陆状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "用户唯一ID", required = true, dataTypeClass = int.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "ToKen", required = true, dataTypeClass = String.class, paramType = "header")
    })
    public Object selectOne(int sid) {
        boolean bool = false;
        try {
            Sign sign = redisForUser.getLogin(sid);
            if (sign != null)
                bool = true;
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnMap(500, "unknown error");
        }
        return ReturnUtils.returnMap(200, bool + "", new HashMap<>());
    }


    /**
     * 电话+密码登录
     *
     * @param Tel      电话号码
     * @param Password 密码
     * @return 单条数据
     */
    @PostMapping("login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Tel", value = "手机号", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "Password", value = "密码", required = true, dataTypeClass = String.class, paramType = "query")
            //@ApiImplicitParam(name = "appId", value = "appId", required = true, dataTypeClass = String.class, paramType = "query")
    })
    public Object login(String Tel, String Password) {
        Map<String, Object> map = new HashMap<>();
        User user;
        try {
            user = this.signService.login(Tel, Password);
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnMap(500, "unknown error");
        }
        //返回Token
        String token = tokenService.getToken(user);
        map.put("token", token);
        user.setUpassword("******"); //隐藏密码
        map.put("user", user);
        return ReturnUtils.returnMap(200, "success", map);
    }

    /**
     * 登出
     *
     * @return Boolean
     */
    @PostMapping("logout")
    @ApiOperation(value = "登出")
    @Token.UserLoginToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", required = true, dataTypeClass = String.class, paramType = "header")
    })
    public Object logout() {
        String token = httpServletRequest.getHeader("token");
        int sid = tokenService.getUidByToken(token);
        boolean result;
        try {
            result = this.signService.logout(sid);
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnMap(500, "unknown error");
        }
        HashMap<String, Object> map = new HashMap<>();
        return ReturnUtils.returnMap(200, "success", map.put("result", result));
    }
}